/**
 * Copyright (C), 2020, 安徽贝慕信息科技有限公司
 * FileName: DealerUserService
 * Author:   Allen
 * Date:     2020/8/26 22:53
 * Description: 商家/系统两用登录用户处理服务类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.bim.marvel.openapi.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.bim.marvel.openapi.client.dto.DealerUserDTO;
import com.bim.marvel.openapi.core.dao.DealerUserDAO;
import com.bim.marvel.openapi.client.enums.QueryTypeEnum;
import com.bim.marvel.openapi.client.query.DealerUserQuery;
import com.bim.marvel.openapi.core.entity.DealerUserDO;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * 〈商家/系统两用登录用户处理服务类〉
 *
 * @author Allen
 * @date 2020/8/26
 * @since 1.0.0
 */
@Service
public class DealerUserService extends ServiceImpl<DealerUserDAO, DealerUserDO> {

    /**
     * 保存用户信息
     * @param dealerUserQuery 商家注册用户信息入参
     * @return 是否注册成功
     */
    public Boolean saveDealerUser(DealerUserQuery dealerUserQuery) {
        DealerUserDO dealerUserDO = new DealerUserDO();
        BeanUtils.copyProperties(dealerUserQuery, dealerUserDO);
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        //如果用户未设置，默认将手机号做为默认密码，前端手机号是做为必填项
        String bcryPassword = bCryptPasswordEncoder.encode(dealerUserQuery.getLoginPassword() == null ? dealerUserQuery
                .getPhone() : dealerUserQuery.getLoginPassword());
        dealerUserDO.setLoginPassword(bcryPassword);
        //开放平台只提供商家用户注册，系统用户目前为初始化好的，仅一个管理员账号
        dealerUserDO.setUserType(1L);
        return save(dealerUserDO);
    }

    /**
     * 根据登录名获得用户信息(因为登录名支持手机号登陆)
     * @param loginName 登陆名
     * @return 用户信息
     */
    public DealerUserDTO getUserByLoginName(String loginName) {
        DealerUserDTO dealerUserDTO =  new DealerUserDTO();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(!loginName.isEmpty(), "login_name", loginName).or(!loginName.isEmpty())
                .eq("phone", loginName);
        DealerUserDO dealerUserDO = getOne(queryWrapper);
        BeanUtils.copyProperties(dealerUserDO, dealerUserDTO);
        return dealerUserDTO;
    }

    /**
     * 根据类型判断登录名/手机号是否已存在
     * @param loginNameOrPhone 登录名或手机号
     * @param queryTypeEnum 查询类型(1:登录名；0：手机号)
     * @return 是否存在(存在返回True,不存在返回False)
     */
    public Boolean getLoginNameOrPhoneExist(String loginNameOrPhone, QueryTypeEnum queryTypeEnum) {
        DealerUserDO dealerUserDO = new DealerUserDO();
        if (queryTypeEnum == QueryTypeEnum.LONGIN_NAME) {
            dealerUserDO.setLoginName(loginNameOrPhone);
        } else if (queryTypeEnum == QueryTypeEnum.PHONE) {
            dealerUserDO.setPhone(loginNameOrPhone);
        } else {
            dealerUserDO.setLoginName(loginNameOrPhone);
        }
        QueryWrapper<DealerUserDO> queryWrapper = new QueryWrapper<>(dealerUserDO);
        return SqlHelper.retBool(count(queryWrapper));
    }
}
